package biolean;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;

import biolean.commands.Filter;

public class FilterForm extends JFrame implements Commons {
	private boolean isClosed = true;
	private String filterOption;
	private String filterValue;
	private Filter mCmdFilter;
	final JTextField input = new JTextField(10);
	
	private final JRadioButton rb_seqName = new JRadioButton(Filter.FILTER_SEQNAME);		
	private final JRadioButton rb_source = new JRadioButton(Filter.FILTER_SOURCE);
	private final JRadioButton rb_feature = new JRadioButton(Filter.FILTER_FEATURE);
	private final JRadioButton rb_start = new JRadioButton(Filter.FILTER_START);
	private final JRadioButton rb_end = new JRadioButton(Filter.FILTER_END);
	private final JRadioButton rb_score = new JRadioButton(Filter.FILTER_SCORE);
	private final JRadioButton rb_strand = new JRadioButton(Filter.FILTER_STRAND);
	private final JRadioButton rb_frame = new JRadioButton(Filter.FILTER_FRAME);

	public boolean isClosed() {
		return isClosed;
	}
	
	public String getFilterOption() {
		return filterOption;
	}
	
	public String getFilterValue() {
		return filterOption;
	}
	
	public void selectExisting()
	{
		if (mCmdFilter.mFilterSubject != null && mCmdFilter.mValue != null)
		{
			if (mCmdFilter.mFilterSubject.equals(Filter.FILTER_SEQNAME) )
				rb_seqName.setSelected(true);
			else if (mCmdFilter.mFilterSubject.equals(Filter.FILTER_FEATURE) )
				rb_feature.setSelected(true);
			else if (mCmdFilter.mFilterSubject.equals(Filter.FILTER_FRAME) )
				rb_frame.setSelected(true);
			else if (mCmdFilter.mFilterSubject.equals(Filter.FILTER_SCORE) )	
				rb_score.setSelected(true);
			else if (mCmdFilter.mFilterSubject.equals(Filter.FILTER_END) )	
				rb_end.setSelected(true);
			else if (mCmdFilter.mFilterSubject.equals(Filter.FILTER_SOURCE) )	
				rb_source.setSelected(true);
			else if (mCmdFilter.mFilterSubject.equals(Filter.FILTER_START) )	
				rb_start.setSelected(true);
			else if (mCmdFilter.mFilterSubject.equals(Filter.FILTER_STRAND) )	
				rb_strand.setSelected(true);
			input.setText( mCmdFilter.mValue );
		}
	}
	
	public FilterForm(Filter CmdFilter) {
		mCmdFilter = CmdFilter;
		final WinAdapter winAdapter = new WinAdapter();
		addWindowListener(winAdapter);
		setTitle("Filter");
		setSize(COMMAND_FORM_WIDTH, COMMAND_FORM_HEIGHT);
		setLocationRelativeTo(null);
		setVisible(true);
		setResizable(false);
		this.setAlwaysOnTop(true);
		isClosed = false;

		setLayout(new BorderLayout());
		// priprava radiobuttonov

		
		rb_seqName.setSelected(true);
		// danie radiobuttonov do groupy
		final ButtonGroup group = new ButtonGroup();
		group.add(rb_seqName);
		group.add(rb_source);
		group.add(rb_feature);
		group.add(rb_start);
		group.add(rb_end);
		group.add(rb_score);
		group.add(rb_strand);
		group.add(rb_frame);

		// pridanie labelu a radiobuttonov
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new FlowLayout());
		add(centerPanel, BorderLayout.CENTER);
		JLabel label_c = new JLabel("Filter by:");
		centerPanel.add(label_c);
		centerPanel.add(rb_seqName);
		centerPanel.add(rb_source);
		centerPanel.add(rb_feature);
		centerPanel.add(rb_start);
		centerPanel.add(rb_end);
		centerPanel.add(rb_score);
		centerPanel.add(rb_strand);
		centerPanel.add(rb_frame);

		// pridanie labelu a textfieldu, hodnota podla kt. sa bude filtrovat
		JPanel northPanel = new JPanel();
		northPanel.setLayout(new FlowLayout());
		add(northPanel, BorderLayout.NORTH);
		JLabel label_n = new JLabel("Value:");
		northPanel.add(label_n);
		northPanel.add(input);
		
		// pridanie buttonu
		JButton b_ok = new JButton("OK");
		
		add(b_ok, BorderLayout.SOUTH);
		selectExisting();
		// listner na button
		b_ok.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				filterOption = null;
				filterValue = input.getText();
				// niekde si pametat subor vstupny a vystupny
				// String pathInFile = null;
				// String pathOutFile = null;
				if (rb_seqName.isSelected() )
					filterOption = Filter.FILTER_SEQNAME;
				else if (rb_source.isSelected() )
					filterOption = Filter.FILTER_SOURCE;
				else if (rb_feature.isSelected() )
					filterOption = Filter.FILTER_FEATURE;
				else if (rb_start.isSelected() )
					filterOption = Filter.FILTER_START;
				else if (rb_end.isSelected() )
					filterOption = Filter.FILTER_END;
				else if (rb_score.isSelected() )
					filterOption = Filter.FILTER_SCORE;
				else if (rb_strand.isSelected() )
					filterOption = Filter.FILTER_STRAND;
				else if (rb_frame.isSelected() )
					filterOption = Filter.FILTER_FRAME;
				mCmdFilter.setArguments(filterOption, filterValue);
				dispose();
				
			}
		});

	}

	private class WinAdapter extends WindowAdapter {
		public void windowClosing(WindowEvent e) {
			isClosed = true;
		}

	}

}
